
package Studentsystem;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @ProjectName: ybxy6
 * @Titile: Main
 * @Author: 86183
 * @Description: TODO
 */
public class Main {
    //创建ArrayList集合对象
    private static final ArrayList<Student> studentList = new ArrayList<>();
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            showMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除输入缓冲区

            switch (choice) {
                case 1 -> addStudent();
                case 2 -> viewStudents();
                case 3 -> deleteStudent();
                case 4 -> modifyStudent();
                case 5 -> queryStudent();
                case 6 -> {
                    System.out.println("退出系统！");
                    System.exit(0);
                }
                default -> System.out.println("输入错误，请重新选择！");
            }
        }
    }
//菜单
    private static void showMenu() {
        System.out.println("----- 学生管理系统 -----");
        System.out.println("1. 添加学生");
        System.out.println("2. 查看所有学生");
        System.out.println("3. 删除学生");
        System.out.println("4. 修改学生");
        System.out.println("5. 查询单个学生");
        System.out.println("6. 退出系统");
        System.out.print("请输入您的选择：");
    }
//添加学生
    private static void addStudent() {
        System.out.println("----- 添加学生 -----");
        String id;
        while (true) {
            System.out.print("请输入学号：");
            id = scanner.nextLine();
            if (getIndex(id) == -1) break;
            System.out.println("学号重复，请重新输入！");
        }

        System.out.print("请输入姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入年龄：");
        int age = scanner.nextInt();
        scanner.nextLine(); // 清除输入缓冲区
        System.out.print("请输入性别：");
        String gender = scanner.nextLine();
        System.out.print("请输入住址：");
        String address = scanner.nextLine();

        Student student = new Student(id, name, gender, age, address);
        studentList.add(student);
        System.out.println("添加成功！");
    }
//查看所有学生
    private static void viewStudents() {
        System.out.println("----- 学生列表 -----");
        if (studentList.isEmpty()) {
            System.out.println("暂无学生信息！");
            return;
        }

        System.out.printf("%-10s %-15s %-5s %-5s %-15s\n",
                "学号", "姓名", "性别", "年龄", "住址");
        for (Student student : studentList) {
            System.out.printf("%-10s %-15s %-5s %-5s %-15s\n",
                    student.getId(),
                    student.getName(),
                    student.getGender(),
                    student.getAge(),
                    student.getAddress());}
        System.out.println("共" + studentList.size() + "位学生");
    }
///删除学生
    private static void deleteStudent() {
        System.out.println("----- 删除学生 -----");
        System.out.print("请输入要删除的学生学号：");
        String id = scanner.nextLine();
        int index = getIndex(id);


        if (index == -1) {
            System.out.println("学号不存在！");
        } else {
            studentList.remove(index);

            System.out.println("删除成功！");

        }
    }
//修改学生
    private static void modifyStudent() {
        System.out.println("----- 修改学生 -----");
        System.out.print("请输入要修改的学生学号：");
        String id = scanner.nextLine();
        int index = getIndex(id);

        if (index == -1) {
            System.out.println("学号不存在！");
        } else {
            Student student = studentList.get(index);
            System.out.print("请输入新姓名（原值：" + student.getName() + "）：");
            student.setName(scanner.nextLine());
            System.out.print("请输入新性别（原值：" + student.getGender() + "）：");
            student.setGender(scanner.nextLine());
            System.out.print("请输入新年龄（原值：" + student.getAge() + "）：");
            student.setAge(scanner.nextLine());
            scanner.nextLine(); // 清除输入缓冲区
            System.out.print("请输入新住址（原值：" + student.getAddress() + "）：");
            student.setAddress(scanner.nextLine());
            System.out.println("修改成功！");
        }
    }
    //查询单个学生
    private static void queryStudent() {
        System.out.println("----- 查询单个学生 -----");
        System.out.print("请输入要查询的学生学号：");
        String id = scanner.nextLine();
        int index = getIndex(id);

        if (index == -1) {
            System.out.println("学号不存在！");
        } else {
            Student student = studentList.get(index);
            System.out.printf("%-5s %-5s %-5s %-5s %-5s\n",
                    "学号", "姓名", "性别", "年龄", "住址");
            System.out.printf("%-5s %-5s %-5s %-5s %-5s\n",
                    student.getId(),
                    student.getName(),
                    student.getGender(),
                    student.getAge(),
                    student.getAddress());
        }
    }
    // 自定义方法：根据学号获取学生索引
    private static int getIndex(String id) {
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}